import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '../stores/user'
import Auth from '../views/Auth.vue'
import Home from '../views/Home.vue'

// Admin Views
import AdminDashboard from '../views/admin/AdminDashboard.vue'
import AdminAnnouncements from '../views/admin/AdminAnnouncements.vue'
import AdminServiceManagement from '../views/admin/AdminServiceManagement.vue'
import AdminTicketProcessing from '../views/admin/AdminTicketProcessing.vue'
import AdminProfile from '../views/admin/AdminProfile.vue'

// User Views
import UserAnnouncements from '../views/user/UserAnnouncements.vue'
import UserServiceForm from '../views/user/UserServiceForm.vue'
import UserTickets from '../views/user/UserTickets.vue'
import UserProfile from '../views/user/UserProfile.vue'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    meta: { requiresAuth: true }
  },
  {
    path: '/auth',
    name: 'Auth',
    component: Auth
  },
  {
    path: '/admin/dashboard',
    name: 'AdminDashboard',
    component: AdminDashboard,
    meta: { requiresAuth: true, role: 'ADMIN' }
  },
  {
    path: '/admin/announcements',
    name: 'AdminAnnouncements',
    component: AdminAnnouncements,
    meta: { requiresAuth: true, role: 'ADMIN' }
  },
  {
    path: '/admin/service',
    name: 'AdminServiceManagement',
    component: AdminServiceManagement,
    meta: { requiresAuth: true, role: 'ADMIN' }
  },
  {
    path: '/admin/tickets',
    name: 'AdminTicketProcessing',
    component: AdminTicketProcessing,
    meta: { requiresAuth: true, role: 'ADMIN' }
  },
  {
    path: '/admin/profile',
    name: 'AdminProfile',
    component: AdminProfile,
    meta: { requiresAuth: true, role: 'ADMIN' }
  },
  {
    path: '/user/announcements',
    name: 'UserAnnouncements',
    component: UserAnnouncements,
    meta: { requiresAuth: true, role: 'USER' }
  },
  {
    path: '/user/submit',
    name: 'UserServiceForm',
    component: UserServiceForm,
    meta: { requiresAuth: true, role: 'USER' }
  },
  {
    path: '/user/tickets',
    name: 'UserTickets',
    component: UserTickets,
    meta: { requiresAuth: true, role: 'USER' }
  },
  {
    path: '/user/profile',
    name: 'UserProfile',
    component: UserProfile,
    meta: { requiresAuth: true, role: 'USER' }
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    redirect: '/'
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  const requiresAuth = to.meta.requiresAuth
  const requiredRole = to.meta.role

  if (requiresAuth && !userStore.isLoggedIn) {
    next('/auth')
  } else if (requiresAuth && requiredRole && userStore.role !== requiredRole) {
    alert('您没有权限访问此页面！')
    next(from.path || '/')
  } else if ((to.path === '/auth' || to.path === '/') && userStore.isLoggedIn) {
    if (userStore.role === 'ADMIN') {
      next('/admin/dashboard')
    } else if (userStore.role === 'USER') {
      next('/user/submit')
    } else {
      next('/')
    }
  } else {
    next()
  }
})

export default router